from database.mysqlhelper import MySqLHelper

db = MySqLHelper()

insert_sql = 'insert into t_funds_info (f_code,f_name,f_ratio,f_hold_hard,f_stock_val,' \
             'f_fund_code,f_fund_name,f_total_val,f_type,f_short_name,f_full_name,f_attribute) ' \
             'VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) '
select_sql = 'select f_code,f_name,f_ratio,f_hold_hard,f_stock_val,' \
             'f_fund_code,f_fund_name,f_total_val,f_type,f_short_name,f_full_name,f_attribute,f_update_time from t_funds_info '


class FundDto:
    def __init__(self, code, name, ratio, hold_hard, stock_val, fund_code, fund_name, total_val,
                 f_type, short_name, full_name, attribute, up_time):
        self.code = code
        self.name = name
        self.ratio = ratio
        self.hold_hard = hold_hard
        self.stock_val = stock_val
        self.fund_code = fund_code
        self.fund_name = fund_name
        self.total_val = total_val
        self.type = f_type
        self.short_name = short_name
        self.full_name = full_name
        self.attribute = attribute
        self.up_time = up_time

    def __repr__(self):
        return repr(
            (self.code, self.name, self.ratio, self.hold_hard, self.stock_val, self.fund_code,
             self.fund_name, self.total_val, self.type, self.short_name, self.full_name, self.attribute))

    def to_tup(self) -> tuple:
        return (self.code, self.name, self.ratio, self.hold_hard, self.stock_val, self.fund_code,
                self.fund_name, self.total_val, self.type, self.short_name, self.full_name, self.attribute)


def format_mul(res_list):
    list = []
    for res in res_list:
        list.append(format_one(res))
    return list


def format_info(res) -> FundDto:
    if not res:
        return None
    return FundDto(str(res[0], encoding="utf-8"), str(res[1], encoding="utf-8"), float(res[2]), float(res[3]),
                   int(res[4]), str(res[5], encoding="utf-8"), str(res[6], encoding="utf-8"), int(res[7]),
                   str(res[8], encoding="utf-8"), str(res[9], encoding="utf-8"), str(res[10], encoding="utf-8"),
                   str(res[11], encoding="utf-8"), res[12])


# 获取所有持有的基金
def select_by_stock_code(code):
    sql = select_sql + ' where f_code = %s '
    return format_mul(db.selectall(sql, (code)))


# 插入多条
def insert(info):
    # 增加多条
    return db.insertone(insert_sql, (
        info['code'], info['name'], info['ratio'], info['hold_hard'], info['stock_val'], info['fund_code'],
        info['fund_name'], info['total_val'], info['type'], info['short_name'], info['full_name'],
        info['attribute']))


# 插入多条
def insert_mul(data_list):
    # 增加多条
    li = []
    for info in data_list:
        li.append((info['code'], info['name'], info['ratio'], info['hold_hard'], info['stock_val'], info['fund_code'],
                   info['fund_name'], info['total_val'], info['type'], info['short_name'], info['full_name'],
                   info['attribute']))
    return db.insertmany(insert_sql, li)


# 获取一条
def select_stock_code(s_code):
    sql = select_sql + ' where f_code = %s '
    return format_res(db.selectall(sql, (s_code)))


# 获取一条
def select_fund_code(f_code):
    sql = select_sql + ' where f_fund_code = %s '
    return format_res(db.selectall(sql, (f_code)))


# 获取一条
def select_fund_one(fund_code):
    sql = select_sql + ' where f_fund_code = %s limit 1 '
    return format_one(db.selectone(sql, (fund_code)))


# 删除一版
def delete_fund(fund_code):
    sql = 'delete from t_funds_info where f_fund_code = %s '
    return db.delete(sql, (fund_code))


# 所有股票
def update_val(stock_code, fund_code, stock_val):
    sql1 = 'update t_funds_info set f_total_val = %s where f_code = %s and f_fund_code= %s '
    return db.update(sql1, (stock_val, stock_code, fund_code))


# 格式化结果
def format_one(res):
    if not res:
        return {}
    return {'code': str(res[0], encoding="utf-8"), 'name': str(res[1], encoding="utf-8"),
            'ratio': float(res[2]), 'hold_hard': float(res[3]), 'stock_val': int(res[4]),
            'fund_code': str(res[5], encoding="utf-8"), 'fund_name': str(res[6], encoding="utf-8"),
            'total_val': int(res[7]), 'type': str(res[8], encoding="utf-8"),
            'short_name': str(res[9], encoding="utf-8"), 'full_name': str(res[10], encoding="utf-8"),
            'attribute': str(res[11], encoding="utf-8"), 'up_time': res[12]}


# 格式化结果
def format_res(res_list):
    list = []
    for res in res_list:
        list.append(format_one(res))
    return list


if __name__ == '__main__':
    select_by_stock_code("600031")